package com.huike.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.huike.constant.Constants;
import com.huike.core.domain.entity.SysUser;
import com.huike.dto.domain.AssignRecord;
import com.huike.dto.domain.RulePool;
import com.huike.dto.domain.TbClue;
import com.huike.mapper.AssignRecordMapper;
import com.huike.mapper.TbBusinessMapper;
import com.huike.mapper.TbClueMapper;
import com.huike.service.IRulePoolService;
import com.huike.service.ITransferService;
import com.huike.system.mapper.SysUserMapper;
import com.huike.system.service.ISysUserService;
import com.huike.utils.DateUtils;
import com.huike.utils.HuiKeCrmDateUtils;
import com.huike.vo.TransferVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TransferServiceImpl implements ITransferService {
    @Autowired
    private ISysUserService userService;

    @Autowired
    private AssignRecordMapper assignRecordMapper;

    @Autowired
    private IRulePoolService rulePoolService;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private TbClueMapper clueMapper;

    @Autowired
    private TbBusinessMapper businessMapper;

    @Override
    public List<TransferVo> selectTransferList(SysUser user) {
        List<TransferVo> transfervoList =new ArrayList<>();
        List<SysUser> list = userService.selectUserList(user);
        for (SysUser sysUser : list) {
            TransferVo transfervo= new TransferVo();
            int clueNum= assignRecordMapper.countAssignCluesByUser(sysUser.getUserId());
            int businessNum= assignRecordMapper.countAssignBusinessByUser(sysUser.getUserId());
            if(clueNum+businessNum>0){
                transfervo.setUserName(sysUser.getUserName());
                transfervo.setClueNum(clueNum);
                transfervo.setBusinessNum(businessNum);
                transfervo.setUserId(sysUser.getUserId());
                transfervo.setPhonenumber(sysUser.getPhonenumber());
                transfervo.setCreateTime( DateUtil.date(sysUser.getCreateTime()));
                transfervoList.add(transfervo);
            }
        }
        return transfervoList;
    }

    @Override
    public Map<String, Object> assignment(String type, Long userId, Long transferUserId) {
        Map<String ,Object> result =new HashMap<>();
        if (AssignRecord.RecordType.CLUES.getValue().equals(type)) {
            int clueNum = assignRecordMapper.countAssignCluesByUser(userId);
            if (clueNum >= 0) {
                RulePool rulePool = rulePoolService.selectRulePoolByType(Constants.rule_type_clue);
                int transferUserClueNum = assignRecordMapper.countAssignCluesByUser(transferUserId);
                // 被转派人保有量达到最大值
                if (transferUserClueNum >= rulePool.getMaxNunmber()) {
                    result.put("flag",false);
                    result.put("msg","线索转换失败！已经达到被转派人最大保有量");
                } else {
                    AssignRecord assignRecord = new AssignRecord();
                    assignRecord.setUserId(userId);
                    assignRecord.setLatest("1");
                    assignRecord.setType(AssignRecord.RecordType.CLUES.getValue());
                    List<AssignRecord> list = assignRecordMapper.selectAssignRecordList(assignRecord);
                    for (int i = 0; i < list.size(); i++) {
                        AssignRecord tbAssignRecord = list.get(i);
                        SysUser sysUser = sysUserMapper.selectUserById(transferUserId);
                        tbAssignRecord.setUserId(transferUserId);
                        tbAssignRecord.setUserName(sysUser.getUserName());
                        tbAssignRecord.setDeptId(sysUser.getDeptId());
                        tbAssignRecord.setCreateTime(DateUtil.toLocalDateTime(DateUtils.getNowDate()));
                        assignRecordMapper.updateAssignRecord(tbAssignRecord);
                        clueMapper.setTransfer(tbAssignRecord.getAssignId(), TbClue.StatusType.UNFOLLOWED.getValue());
                        Date endDate = HuiKeCrmDateUtils.getEndDateByRule(tbAssignRecord);
                        clueMapper.updateClueEndTimeById(tbAssignRecord.getAssignId(),endDate);
                        if (transferUserClueNum + i >= rulePool.getMaxNunmber()) {
                            result.put("flag",false);
                            result.put("msg","线索转换失败！已经分配" + i + " 线索");
                            break;
                        }
                    }
                }
            }
        }
        if (AssignRecord.RecordType.BUSNIESS.getValue().equals(type)) {
            int busniessNum = assignRecordMapper.countAssignBusinessByUser(userId);
            if (busniessNum >= 0) {
                RulePool rulePool = rulePoolService.selectRulePoolByType(Constants.rule_type_business);
                int transferUserBusinessNum = assignRecordMapper.countAssignBusinessByUser(transferUserId);
                //被转派人保有量达到最大值
                if (transferUserBusinessNum >= rulePool.getMaxNunmber()) {
                    result.put("flag",false);
                    result.put("msg","线索转换失败！已经达到被转派人最大保有量");
                } else {
                    AssignRecord assignRecord = new AssignRecord();
                    assignRecord.setUserId(userId);
                    assignRecord.setLatest("1");
                    assignRecord.setType(AssignRecord.RecordType.BUSNIESS.getValue());
                    List<AssignRecord> list = assignRecordMapper.selectAssignRecordList(assignRecord);
                    for (int i = 0; i < list.size(); i++) {
                        AssignRecord tbAssignRecord = list.get(i);
                        SysUser sysUser = sysUserMapper.selectUserById(transferUserId);
                        tbAssignRecord.setUserId(transferUserId);
                        tbAssignRecord.setUserName(sysUser.getUserName());
                        tbAssignRecord.setDeptId(sysUser.getDeptId());
                        tbAssignRecord.setCreateTime(DateUtil.toLocalDateTime(DateUtils.getNowDate()));
                        assignRecordMapper.updateAssignRecord(tbAssignRecord);
                        Date endDate = HuiKeCrmDateUtils.getEndDateByRule(tbAssignRecord);
                        businessMapper.updateBusinessEndTimeById(tbAssignRecord.getAssignId(),endDate);
                        if (transferUserBusinessNum + i >= rulePool.getMaxNunmber()) {
                            result.put("flag",false);
                            result.put("msg","商机转换失败！已经转派" + i + " 商机");
                            break;
                        }
                    }
                }
            }
        }
        return result;
    }
}
